var ws, uid, info, parentPg;
document.addEventListener('plusready',function(){
	ws = plus.webview.currentWebview();
	uid = plus.storage.getItem('uid');
	parentPg = plus.webview.getWebviewById('index.html');
	//添加提醒
	$('.addwarn').click(function(){
		goNewPage('me-remind-add.html',{spage:ws.id})
	})
	searchWarn();
	showWating();
},false)
//查询数据库
function searchWarn(){
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=medicinal&a=warnList",
		data :{
			uid :uid 
		},
		success :function(data){
			plus.storage.setItem('warnlist',data);
			info = $.parseJSON(data); 
//			console.log(JSON.stringify(data));
			plus.nativeUI.closeWaiting();
			//
			warnList(); 
			//开启关闭
			$('.mui-switch').on('tap',function(){
				if(this.classList.contains('mui-active')){//关闭
					ocwarn(0, $(this).attr('dataid'));
				}else{//开启
					ocwarn(1, $(this).attr('dataid'));
				}
			})
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
}
//document.addEventListener( "pause", searchWarn, false );
var wk, cc;
//加载提醒列表
function warnList(){
	//获取当前 时 分
	var d = new Date(); //实例一个时间对象；
	var h = parseInt(d.getHours()); //获取系统时，
	var m = parseInt(d.getMinutes()); //分
	var w = parseInt(d.getDay());//星期
	switch(w){
		case 0:wk = '星期日';break;
		case 1:wk = '星期一';break;
		case 2:wk = '星期二';break;
		case 3:wk = '星期三';break;
		case 4:wk = '星期四';break;
		case 5:wk = '星期五';break;
		case 6:wk = '星期六';break;
	}
	var ntime = (h*60) + m;//现在总分钟
	var list = '';
	if(info.length > 0){ 
		$.each(info, function(k,v) {
			if(v.switchs == 1){
				var hours = parseInt(v.hours);//用户储存的小时数-》几点
				var minutes = parseInt(v.minutes);//用户储存的分钟数-》几分
				var ttime = (hours*60) + minutes//总分钟
				var jtime = ttime - ntime;//用户设置的时间减去当前时间的分钟数
				var ww = v.week.split(' '); //用户选择的星期-》用空格隔开
				if(w == 0)w=7;//w当天星期数
				var tian = 0;
				var x = [];//用户选的星期数组
				for(var i = 0; i < ww.length -1; i++){ 
					switch(ww[i]){
						case '星期日':vk = 0;x.push(vk);break;//vk星期数
						case '星期一':vk = 1;x.push(vk);break;
						case '星期二':vk = 2;x.push(vk);break;
						case '星期三':vk = 3;x.push(vk);break;
						case '星期四':vk = 4;x.push(vk);break;
						case '星期五':vk = 5;x.push(vk);break;
						case '星期六':vk = 6;x.push(vk);break;
					}
				}
				for(var i=0;i<x.length;i++){
					console.log(x[i]);
					if(x[i] == w){
						if(jtime < 0){
							/**
							 * 有今天，但是时间已经过了
							 * 在原有的基础上减1天
							 */
//							tian = 7 - w + x[i];
							if(!x[i+1]){ //如果今天为最后一天
								tian = 7 - w +x[0];break;//若是最后一个则返回第一个
							}else{
								tian = x[i+1] - w;
							}
						}else{
							tian = 0;
						}
						break;
					}else if(x[i] > w){
						tian = x[i] - w;//相差几天
						break;
					}else if(x[i] < w){
						tian = 7 - w +x[i]; 
		//				break;
					}
					if(!x[i+1]){ 
		//				x[i] = x[0];
						tian = 7 - w +x[0];break;
					}//若是最后一个则返回第一个
		        }
				var next_sz = tian != '0'?(tian - 1) * 60 * 24 + ( 24 * 60 - ntime) + ttime:(7 - 1) * 60 * 24 + ( 24 * 60 - ntime) + ttime;
//				var next_sz = (tian - 1) * 60 * 24 + ( 24 * 60 - ntime) + ttime;
//				console.log(tian+" "+next_sz);
				//先求天  
				var s_day = parseInt(next_sz/60/24);
				//再求时
				var sc = (next_sz -  s_day * 60 *24)/60;
				var s_hour = parseInt(sc); 
				//最后求分钟
				var s_minute = next_sz - parseInt(s_day * 60 *24) - (parseInt((next_sz -  s_day * 60 *24)/60) * 60);
				console.log(s_day+" "+s_hour+" "+s_minute); 
				if(wk == ww[i]){//当星期有符合该条提醒的星期
					if(jtime == 0){//今天的时间和用户时间一致，也就是时间到了，则提醒
						var mm = '0天0小时0分钟';
					}else if(jtime < 0){//时间已过 
						mm = s_day + "天" + s_hour + "小时" + s_minute + "分";
					}else if(jtime > 0){
						//今日响铃还未到
						mm = jtime/60 >= 1?parseInt(jtime/60)+'小时'+(jtime - (parseInt(jtime/60)*60))+'分钟后响铃':'0小时'+jtime+"分钟后响铃";
					}
				}else{
					mm = s_day + "天" + s_hour + "小时" + s_minute + "分"; 
				}
				console.log(mm); 
			}
			var active = v.switchs == 1 ? "mui-active" : "" ; 
			if(v.tid == '1'){
				list += '<div class="mui-table-view mui-table-view-cell bbs-ico liwarn'+v.id+'">'+
							'<div class="overflow-h mui-slider-handle">'+
								'<i><li class="remind-list"><span>'+v.hours+":"+v.minutes+'</span></li></i>'+
								'<div class="goinfo"  data-id="'+v.id+'">'+
									'<li class="mui-h6 "><span>'+v.week+'</span></li>'+
									'<li class="mui-h6 "><span class="shi'+v.id+'">'+((v.switchs == 1)?mm:'未开启')+'</span></li>'+
								'</div>'+
								'<li>'+
									'<div class="mui-switch mui-switch-mini '+active+' swi'+v.id+'" dataid="'+v.id+'">'+
										'<div class="mui-switch-handle"></div>'+
									'</div>'+  
								'</li>'+
							'</div>'+
							'<div class="mui-slider-right mui-disabled" onclick="delwarn(' + v.id + ')">'+
								'<span class="mui-btn mui-btn-red">删除</span>'+
							'</div>'+
						'</div>';
			}else if(v.tid == '2'){ 
				console.log(ntime);
				list += '<div class="mui-table-view mui-table-view-cell bbs-ico liwarn'+v.id+'">'+
							'<div class="overflow-h mui-slider-handle">'+
								'<i><img src="'+getAvatar(v.davatar)+'"/></i>'+
								'<div class="dinfo"  data-id="'+v.id+'">'+
									'<li class="font-14">'+
										'<span>'+v.doctor+'</span>'+
									'</li>'+
									'<li class="mui-h6 ">'+ 
										'<span class="shi'+v.id+'">';
								if(v.switchs == 1){
									if(v.time == '饭前' && v.num == '一天两次'){
										list += '提醒时间：07:30&nbsp;17:30';
										var zaotime = 7*60+30;
										var wantime = 17*60+30;
										console.log(zaotime+" "+wantime);
									}else if(v.time == '饭后' && v.num == '一天两次'){
										list += '提醒时间：08:30&nbsp;18:30';
										var zaotime = 8*60+30;
										var wantime = 18*60+30;
										console.log(zaotime+" "+wantime);
									}else if(v.time == '饭前' && v.num == '一天三次'){
										list += '提醒时间：07:30&nbsp;11:30&nbsp;17:30';
										var zaotime = 7*60+30;
										var zongtime = 11*60+30;
										var wantime = 17*60+30;
										console.log(zaotime+" "+wantime);
									}else if(v.time == '饭后' && v.num == '一天三次'){
										list += '提醒时间：08:30&nbsp;12:30&nbsp;18:30';
										var zaotime = 8*60+30;
										var zongtime = 12*60+30;
										var wantime = 18*60+30;
									}
									console.log(ntime+" "+zaotime+" "+zongtime+" "+wantime);
								}else{
									list += '未开启';
								}
								list += '</span>'+
									'</li>'+
								'</div>'+
								'<li>'+
									'<div class="mui-switch mui-switch-mini '+active+' swi'+v.id+'" dataid="'+v.id+'">'+
										'<div class="mui-switch-handle"></div>'+
									'</div>'+
								'</li>'+
							'</div>'+
							'<div class="mui-slider-right mui-disabled" onclick="delwarn(' + v.id + ')">'+
								'<a class="mui-btn mui-btn-red">删除</a>'+
							'</div>'+
						'</div>';
			}
		});
		$('.warnlist').html(list); 
		//进入编辑提醒
		$('.goinfo').click(function(){
			goNewPage('me-remind-bianji.html',{spage:ws.id,aid:$(this).attr('data-id')})
		})
		//进入医生编辑提醒
		$('.dinfo').click(function(){
			goNewPage('me-remind-doctor.html', {spage:ws.id,aid:$(this).attr('data-id')})
		})
	}
}
//定时加载提醒列表
var tt = setInterval(warnList,30000); 
//删除提醒
function delwarn(id){
	console.log(id);
	showWating();
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=medicinal&a=delWarn",
		data :{
			id :id
		},
		dataType :'json',
		success :function(data){
			plus.nativeUI.closeWaiting();
			console.log(JSON.stringify(data));
			if(data > 0){
				toast('删除成功');  
				$('.liwarn'+id).remove();
				parentPg.evalJS('searchWarn()');
				searchWarn();
			}
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
}
//开启关闭提醒
function ocwarn(witch, id){
	console.log(witch+" "+id);  
//	console.log(apiRoot+"?m=home&c=medicinal&a=saveWarn&id="+id+"&witch="+witch);return;
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=medicinal&a=saveWarn",
		data :{
			id :id,
			witch :witch,
			kai :1
		},
		dataType :'json',
		success :function(data){
			plus.nativeUI.closeWaiting();
			console.log(JSON.stringify(data));
			if(data > 0){
				toast('修改成功');
				if(witch == 1){
					$('.swi'+id).addClass('mui-active');
					parentPg.evalJS('searchWarn()');
					searchWarn();
				}else{
					$('.swi'+id).removeClass('mui-active');
					$('.shi'+id).text('未开启');
					parentPg.evalJS('searchWarn()');
					parentPg.evalJS('stopPlay("' + id + '")');
					searchWarn();
				}
			}else{
				toast('修改失败');
			}
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
}
